package com.estate.struts.actions;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import com.estate.business.TenderSuccService;
import com.estate.utils.ProjectConstants;
import com.estate.vo.Vo;
import com.estate.vo.impl.TenderSuccVo;
import com.estate.vo.impl.TenderVo;

public class TenderSuccAction extends EstateSupport {

	  private String id;
		 private String tenderId;
		 private String succQuali;
		 private String mgrName;
		 private String mgrQuali;
		 private String succMoney;
		 private String realMoney;
		 private String succDate;
		 private String succRate;
		 private String remark;
		 private String handleName;
		 private String handlePhone;
		 private String deadLineDate;
		 private String succCompany;
	
	private String method;
	
	private String multiType;
	
	private String multiDelete;
	private String delStr;
	
	private TenderSuccService tenderSuccService;
	
	private String directPage;
	@Override
	public String execute() throws Exception {
		System.out.println("method ================= " + this.getMethod());
		System.out.println(this.getId());
		String page = "";
		if("add".equals(this.getMethod())){
			TenderSuccVo tenderSucc = this.getAddTenderSucc();
			if(tenderSucc != null){
				boolean result = this.getTenderSuccService().addTenderSucc(tenderSucc);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}else{
				page="fail";
			}
		}else if("list".equals(this.getMethod())){
			List<Vo> list = this.getTenderSuccService().getList();
			this.getRequest().setAttribute("list", list);
			if(this.getMultiType() != null && this.getMultiType().equals("true")){
				this.getRequest().setAttribute("multiType", new Boolean(true));
			}else{
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			page="allList";
		}else if("detail".equals(this.getMethod())){
			Vo vo = this.getTenderSuccService().getTenderSucc(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("tenderSucc", vo);
			page = "detail";
		}else if("modify".equals(this.getMethod())){
			Vo vo = this.getTenderSuccService().getTenderSucc(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("tenderSucc", vo);
			page = "update";
		}else if("update".equals(this.getMethod())){
			TenderSuccVo tenderSucc = this.getUpdateTenderSucc();
			if(tenderSucc == null){
				page = "fail";
			}else{
				boolean result = this.getTenderSuccService().updateTenderSucc(tenderSucc);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}
		}
//		else if("post".equals(this.getMethod())){
//			this.getAnnouncementService().updatePost(Integer.parseInt(this.getId()));
//			page = this.SUCCESS;
//		}
		else if("delete".equals(this.getMethod())){
			if("true".equals(this.multiDelete)){
//				System.out.println(this.getDelStr());
//				System.out.println(this.getDelStr().split(",").length);
				boolean result = this.getTenderSuccService().deleteMultiTenderSucc(this.getDelStr());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
				
			}else{
//				System.out.println("single delete");
				if(this.getId() == null || "".equals(this.getId())){
					page="faild";
				}else{
					TenderSuccVo tenderSucc = new TenderSuccVo();
					tenderSucc.setId(Integer.parseInt(this.getId()));
					boolean result = this.getTenderSuccService().deleteTenderSucc(tenderSucc);
					if(result){
						page=this.SUCCESS;
					}else{
						page="fail";
					}
				}
			}
		}else{
			page="fail";
		}
		if(this.getDirectPage() != null && !"".equals(this.getDirectPage())){
			page = this.getDirectPage();
		}
		return page;
	}
	private TenderSuccVo getUpdateTenderSucc(){
		TenderSuccVo tenderSucc = new TenderSuccVo();
		
		int id = Integer.parseInt(this.getId());
		try {
			tenderSucc.setId(id);
			tenderSucc.setHandleName(this.getHandleName());
			tenderSucc.setHandlePhone(this.getHandlePhone());
			tenderSucc.setMgrName(this.getMgrName());
			tenderSucc.setMgrQuali(this.getMgrQuali());
			tenderSucc.setRealMoney(this.getRealMoney());
			tenderSucc.setRemark(this.getRemark());
			tenderSucc.setSuccDate(this.getSuccDate());
			tenderSucc.setSuccMoney(this.getSuccMoney());
			tenderSucc.setSuccQuali(this.getSuccQuali());
			tenderSucc.setSuccRate(this.getSuccRate());
			tenderSucc.setSuccCompany(this.getSuccCompany());
			tenderSucc.setDeadLineDate(ProjectConstants.SIMPLE_DATE.parse(this.getDeadLineDate()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tenderSucc;
	}
	private TenderSuccVo getAddTenderSucc(){
		TenderSuccVo tenderSucc = new TenderSuccVo();
		
		try {
			tenderSucc.setHandleName(this.getHandleName());
			tenderSucc.setHandlePhone(this.getHandlePhone());
			tenderSucc.setMgrName(this.getMgrName());
			tenderSucc.setMgrQuali(this.getMgrQuali());
			tenderSucc.setPostDate(new Date());
			tenderSucc.setPostUser(this.getSessionUser());
			tenderSucc.setRealMoney(this.getRealMoney());
			tenderSucc.setRemark(this.getRemark());
			tenderSucc.setSuccDate(this.getSuccDate());
			tenderSucc.setSuccMoney(this.getSuccMoney());
			tenderSucc.setSuccQuali(this.getSuccQuali());
			tenderSucc.setSuccRate(this.getSuccRate());
			tenderSucc.setDeadLineDate(ProjectConstants.SIMPLE_DATE.parse(this.getDeadLineDate()));
			tenderSucc.setSuccCompany(this.getSuccCompany());
			
			TenderVo tender = new TenderVo();
			tender.setId(Integer.parseInt(this.getTenderId()));
			tenderSucc.setTender(tender);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		return tenderSucc;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTenderId() {
		return tenderId;
	}
	public void setTenderId(String tenderId) {
		this.tenderId = tenderId;
	}
	public String getSuccQuali() {
		return succQuali;
	}
	public void setSuccQuali(String succQuali) {
		this.succQuali = succQuali;
	}
	public String getMgrName() {
		return mgrName;
	}
	public void setMgrName(String mgrName) {
		this.mgrName = mgrName;
	}
	public String getMgrQuali() {
		return mgrQuali;
	}
	public void setMgrQuali(String mgrQuali) {
		this.mgrQuali = mgrQuali;
	}
	public String getSuccMoney() {
		return succMoney;
	}
	public void setSuccMoney(String succMoney) {
		this.succMoney = succMoney;
	}
	public String getRealMoney() {
		return realMoney;
	}
	public void setRealMoney(String realMoney) {
		this.realMoney = realMoney;
	}
	public String getSuccDate() {
		return succDate;
	}
	public void setSuccDate(String succDate) {
		this.succDate = succDate;
	}
	public String getSuccRate() {
		return succRate;
	}
	public void setSuccRate(String succRate) {
		this.succRate = succRate;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public String getHandleName() {
		return handleName;
	}
	public void setHandleName(String handleName) {
		this.handleName = handleName;
	}
	public String getHandlePhone() {
		return handlePhone;
	}
	public void setHandlePhone(String handlePhone) {
		this.handlePhone = handlePhone;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getMultiType() {
		return multiType;
	}
	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}
	public String getMultiDelete() {
		return multiDelete;
	}
	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}
	public String getDelStr() {
		return delStr;
	}
	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}
	public TenderSuccService getTenderSuccService() {
		return tenderSuccService;
	}
	public void setTenderSuccService(TenderSuccService tenderSuccService) {
		this.tenderSuccService = tenderSuccService;
	}
	public String getDirectPage() {
		return directPage;
	}
	public void setDirectPage(String directPage) {
		this.directPage = directPage;
	}
	public String getDeadLineDate() {
		return deadLineDate;
	}
	public void setDeadLineDate(String deadLineDate) {
		this.deadLineDate = deadLineDate;
	}
	public String getSuccCompany() {
		return succCompany;
	}
	public void setSuccCompany(String succCompany) {
		this.succCompany = succCompany;
	}
}
